package com.liyujie.networkrequest.pop;

import android.content.Context;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.dyhdyh.support.countdowntimer.CountDownTimerSupport;
import com.dyhdyh.support.countdowntimer.OnCountDownTimerListener;
import com.liyujie.networkrequest.R;
import com.liyujie.networkrequest.listener.BeCommonCallback;
import com.liyujie.networkrequest.mobile.event.MessageEvent;
import com.lxj.xpopup.core.CenterPopupView;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

public class SwipeCardPopup  extends CenterPopupView {

    /**
     * 倒计时
     */
    private CountDownTimerSupport mCountDownTimerSupport = new CountDownTimerSupport(10000,1000);



    public SwipeCardPopup(@NonNull Context context) {
        super(context);
        //注册，重复注册会导致崩溃
        EventBus.getDefault().register(this);
    }

    @Override
    protected int getImplLayoutId() {
        return R.layout.popup_swioe_card;

    }


    @Subscribe(threadMode = ThreadMode.MAIN, sticky = true)
    public void onMessageEvent(MessageEvent mMessageEvent) {
        switch (mMessageEvent.getType()){
            case SwipeCardContent:
                /**
                 * 刷卡信息
                 */
                String SwipeCardContent = mMessageEvent.getString();
                if(null != beCommonCallback){
                    beCommonCallback.onBeCommonCallback(SwipeCardContent);
                    try {
                        dismiss();
                    }catch (Exception e){
                        e.getMessage();
                    }
                }
                break;
        }
    }


    @Override
    protected void onCreate() {
        super.onCreate();
        TextView Content = findViewById(R.id.Content);
        Content.setText("请刷卡");

        TextView Countdown = findViewById(R.id.Countdown);
        //倒计时监听
        mCountDownTimerSupport.setOnCountDownTimerListener(new OnCountDownTimerListener() {
            @Override
            public void onTick(long millisUntilFinished) {
                Countdown.setText(millisUntilFinished / 1000 + "s");
            }

            @Override
            public void onFinish() {
                try {
                    dismiss();
                }catch (Exception e){
                    e.getMessage();
                }
            }
        });
        /**
         * 重置时间
         */
        ResetTime();

    }

    /**
     * 重置时间
     */
    protected void ResetTime(){
        if(null != mCountDownTimerSupport){
            mCountDownTimerSupport.reset();
            mCountDownTimerSupport.start();
        }
    }

    /**
     * 重置暂停
     */
    protected void ResetPause(){
        if(null != mCountDownTimerSupport){
            mCountDownTimerSupport.reset();
            mCountDownTimerSupport.pause();
        }
    }



    private BeCommonCallback beCommonCallback;
    public void setBeCommonCallback(BeCommonCallback beCommonCallback) {
        this.beCommonCallback = beCommonCallback;
    }


    @Override
    public void dismiss() {
        if(null != mCountDownTimerSupport){
            mCountDownTimerSupport.reset();
            mCountDownTimerSupport = null;
        }
        //解绑
        EventBus.getDefault().unregister(this);
        super.dismiss();
    }
}

